﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Framework.Storge
{
    public class LocalFile
    {
        public static void Write(String FatherRoot, String ChildRoot, String Name, Byte[] Buffer)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                var FileName = System.IO.Path.Combine(FatherRoot, ChildRoot, Name);

                if (!store.DirectoryExists(FatherRoot))
                {
                    store.CreateDirectory(FatherRoot);
                }
                if (!store.DirectoryExists(System.IO.Path.Combine(FatherRoot, ChildRoot)))
                {
                    store.CreateDirectory(System.IO.Path.Combine(FatherRoot, ChildRoot));
                }

                
                try
                {
                    using (IsolatedStorageFileStream stream =
                        new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, store))
                    {
                        stream.Write(Buffer, 0, Buffer.Length);
                        stream.Close();
                        stream.Dispose();
                        store.Dispose();
                    }
                }
                catch (IsolatedStorageException ex)
                {
                    throw ex;
                }
            }
        }

        public static Byte[] Read(String FatherRoot, String ChildRoot, String Name)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                
                var FileName = System.IO.Path.Combine(FatherRoot, ChildRoot, Name);
                try
                {
                    using (IsolatedStorageFileStream stream =
                        new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, store))
                    {
                        Byte[] buffer = new Byte[stream.Length];
                        stream.Read(buffer, 0, buffer.Length);
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.Close();
                        stream.Dispose();
                        store.Dispose();
                        return buffer;
                    }
                }
                catch (IsolatedStorageException ex)
                {
                    throw ex;
                }
            }
        }

        public static void IncreaseQuota(long QuotaSize,long MinQuotaSize)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                long maxspace = store.AvailableFreeSpace;
                if (maxspace < MinQuotaSize)
                {
                    var result = MessageBox.Show("独立储存空间不足，程序将尝试增加独立储存空间。稍后将由系统弹出请求框，请予以确认。如果您选择不增加独立储存空间，程序将把内容存至内存，这将极大地降低程序执行效率，因此强烈建议您增加独立存储空间！", "独立储存空间不足", MessageBoxButton.OK);
                    if (result != null && !String.IsNullOrEmpty(result.ToString()))
                    {
                        try
                        {
                            store.IncreaseQuotaTo(QuotaSize);
                        }
                        catch
                        {
                            VariableHelper.UseLocalFile = false;
                        }
                    }
                }
                store.Dispose();
            }
        }

        public static void IncreaseQuotaTick()
        {
            long aq = 0;
            long mq = 0;
            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                aq = store.AvailableFreeSpace;
                mq = store.Quota;
                store.Dispose();
            }
            IncreaseQuota(mq + 10485760, 10485760);
        }

        public static bool Exsit(String FatherRoot, String ChildRoot, String Name)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                var FileName = System.IO.Path.Combine(FatherRoot, ChildRoot, Name);
                return store.FileExists(FileName);
            }
        }
    }
}
